package com.gccloud.design.service.impl;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gccloud.design.dao.SdRepoActionConfDao;
import com.gccloud.design.entity.SdRepoActionConfEntity;
import com.gccloud.design.service.SdRepoActionConfService;
import com.gccloud.design.vo.SdActionConfVo;
import com.gccloud.design.vo.SdRepoActionConfVo;
import com.gccloud.starter.core.vo.PageVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * 环节操作配置
 *
 * @author 冯海琴
 */
@Slf4j
@Service
public class SdRepoActionConfServiceImpl extends ServiceImpl<SdRepoActionConfDao, SdRepoActionConfEntity> implements SdRepoActionConfService {

    @Resource
    private SdRepoActionConfDao sdRepoActionConfDao;

    /**
     * 环节操作配置：获取环节操作按钮列表
     */
    @Override
    public Map<String, Object> getLinkButton(String tacheId, PageVO pageObj) {
        Map<String, Object> map = new HashMap<>(4);
        Page<SdActionConfVo> page = new Page<>(pageObj.getCurrent(), pageObj.getSize());
        map.put("list", sdRepoActionConfDao.getLinkButton(tacheId, page));
        map.put("total", page.getTotal());
        return map;
    }

    /**
     * 环节操作配置：查询环节未配置的操作按钮
     */
    @Override
    public Map<String, Object> queryButtonByTacheId(String tacheId, PageVO pageObj) {
        Map<String, Object> map = new HashMap<>(4);
        Page<SdActionConfVo> page = new Page<>(pageObj.getCurrent(), pageObj.getSize());
        map.put("list", sdRepoActionConfDao.queryButtonByTacheId(tacheId, page));
        map.put("total", page.getTotal());
        return map;
    }

    /**
     * 环节操作配置：为某个环节添加按钮,获取当前环节最后显示的按钮显示顺序
     */
    @Override
    public Integer getMaxShowLinkButton(String tacheId) {
        return sdRepoActionConfDao.getMaxShowLinkButton(tacheId);
    }

    /**
     * 环节操作配置：为某个环节添加按钮
     */
    @Override
    public boolean addButtonToLink(List<SdRepoActionConfEntity> linkRelButtonList) {
        int result = 0;
        try {
            result = sdRepoActionConfDao.addButtonToLink(linkRelButtonList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result > 0;
    }

    /**
     * 环节操作配置:删除环节按钮
     */
    @Override
    public boolean deleteButtonToLink(String tacheId, String ids) {
        int result = 0;
        String[] arrStr = ids.split(",");
        Integer[] arr = Convert.toIntArray(arrStr);
        try {
            result = sdRepoActionConfDao.deleteButtonToLink(tacheId, arr);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("---------------------------删除环节按钮失败，" + e.getMessage());
        }
        return result > 0;
    }

    /**
     * 环节操作配置：按钮上移
     */
    @Override
    public boolean upMove(String tacheId, Integer id) {
        List<SdRepoActionConfVo> list1 = sdRepoActionConfDao.getShowSort(tacheId, id);
        if (list1.size() == 0) {
            return false;
        }
        List<SdRepoActionConfVo> list2 = sdRepoActionConfDao.getMinShowSort(tacheId, list1.get(0));
        if (list2.size() == 0) {
            return false;
        }
        //交换显示顺序
        int tmp = list1.get(0).getDisplayOrder();
        list1.get(0).setDisplayOrder(list2.get(0).getDisplayOrder());
        list2.get(0).setDisplayOrder(tmp);

        int result = 0;
        try {
            List<SdRepoActionConfVo> list = new LinkedList<>();
            list.add(list1.get(0));
            list.add(list2.get(0));
            result = sdRepoActionConfDao.exchangeShow(list);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("---------------------------按钮上移失败，" + e.getMessage());
        }
        return result > 0;
    }

    /**
     * 环节操作配置：按钮下移
     */
    @Override
    public boolean downMove(String tacheId, Integer id) {
        List<SdRepoActionConfVo> list1 = sdRepoActionConfDao.getShowSort(tacheId, id);
        if (list1.size() == 0) {
            return false;
        }
        List<SdRepoActionConfVo> list2 = sdRepoActionConfDao.getMaxShowSort(tacheId, list1.get(0));
        if (list2.size() == 0) {
            return false;
        }
        //交换显示顺序
        int tmp = list1.get(0).getDisplayOrder();
        list1.get(0).setDisplayOrder(list2.get(0).getDisplayOrder());
        list2.get(0).setDisplayOrder(tmp);
        int result = 0;
        try {
            List<SdRepoActionConfVo> list = new LinkedList<>();
            list.add(list1.get(0));
            list.add(list2.get(0));
            result = sdRepoActionConfDao.exchangeShow(list);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("---------------------------按钮下移失败，" + e.getMessage());
        }
        return result > 0;
    }

    /**
     * 根据环节id获取按钮列表
     */
    @Override
    public Map<String, Object> getButton(String repoId) {
        Map<String, Object> map = new HashMap<>(4);
        List<SdActionConfVo> list = new ArrayList<>();
        if (repoId != null && !repoId.equals("")) {
            list = sdRepoActionConfDao.getButton(repoId);
        }
        map.put("list", list);
        return map;
    }
}
